<template>
	<view  style="padding:15rpx 20rpx;">
		
		<view class="ni" v-if="from!='detail'">
			<view style="display: flex;align-items: center;">
				<view @tap='openinfo()' style="padding: 20rpx 20rpx; text-align: center;">
					<image style="width: 80rpx;height: 80rpx;border-radius: 10rpx;" :src="image_cache(userinfo.avatar)">
					</image>
					<view style="margin-left: 10rpx;color: #777;">{{userinfo.showname}}</view>
				</view>
			</view>
		</view>
		<view class="ni">
			<view class="ni_cell">
				<view>备注名</view>
				<view class="ni_cell">
					<input v-model="markname" placeholder="请输入备注" class="hui" style="text-align: right; font-size: 26rpx;" />
					<uni-icons type='arrowright' style="margin-top: 5rpx;" color="#333"></uni-icons>
				</view>
			</view>
		</view>
		<!-- <view class="ni">
			<view class="ni_cell">
				<view>查看转账记录</view>
				<uni-icons type='arrowright' style="margin-top: 5rpx;" color="#333"></uni-icons>
			</view>
		</view> -->
		<view class="ni">
			<view class="ni_cell" @click="chattingRecords">
				<view>查找聊天记录</view>
				<uni-icons type='arrowright' style="margin-top: 5rpx;" color="#333"></uni-icons>
			</view>
		</view>
		<view class="ni">
			<view class="ni_cell">
				<view>置顶聊天</view>
				<switch :checked="istop" @change="user_setmenu('top')" style="vertical-align: middle;" />
			</view>
			<view class="ni_cell">
				<view>消息免打扰</view>
				<switch :checked="no_tip" @change="user_setmenu('tip')" style="vertical-align: middle;" />
			</view>
			<view class="ni_cell">
				<view>加入黑名单</view>
				<switch :checked="backlist" @change="user_setmenu('backlist')" style="vertical-align: middle;" />
			</view>
		</view>

		<view class="ni" v-if="userinfo.from!='system'">
			<view class="ni_cell" style=" display: flex;justify-content: center;align-items: center;color: #007AFF;" @tap="deleteFriend">
				<view>
					删除好友
				</view>
			</view>
		</view>
		<view class="ni" @tap="sub_rename()">
			<view class="ni_cell"
				style="border-radius: 20rpx; background: #0692fc; color: #fff; display: flex;justify-content: center;align-items: center;">
				<view>
					修改备注
				</view>
			</view>
		</view>



	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import api from '../../library/index.js'
	import http from '../../library/http.js'
	import uniIcons from '../../components/uni-icons/uni-icons.vue'
	export default {
		components: {
			uniIcons

		},
		data() {
			return {
				system: uni.getStorageSync('system'),
				userinfo: [],
				group_id: 0,
				userid: 0,
				myid: uni.getStorageSync('access_token'),
				group_users: [],
				group_user: [],
				thistype: '',
				usertype: '',
				markname: '',
				istop: false,
				no_tip: false,
				backlist: false,
				myuser: uni.getStorageSync('userInfo'),
				from: '',
				fen:false
			}
		},
		computed: {

		},
		methods: {
			// 聊天记录
			chattingRecords(){
				uni.navigateTo({
					url:'/pages/friend/chattingRecords/chattingRecords'
				})
			},
			// 撤回
			chehui(){
				uni.showModal({
					title:'温馨提示',
					content:'确定清空自己和对方的所有聊天记录？',
					success: (res) => {
						if(res.confirm){
							console.log('')
						}
					}
				})
			},
			user_setmenu(type) {
				if (type == 'top') {
					this.istop = !this.istop;
					var msgtop = uni.getStorageSync('msgtop') ? uni.getStorageSync('msgtop') : [];
					if (this.istop) {
						msgtop.push('U' + this.userid)
					} else {
						for (var i = 0; i < msgtop.length; i++) {
							if (msgtop[i] == 'U' + this.userid) {
								msgtop.splice(i, 1)
							}
						}
					}
					uni.setStorageSync('msgtop', msgtop);
					this.$store.commit('chat/set_istop');
					var data = {
						cache_key: 'U' + this.userid,
						userid: this.myid,
						istop: this.istop
					};

					http.setWait(false).get('group.php?act=set_msgtop', data).then(res => {

					})
				}
				if (type == 'tip') {
					this.no_tip = !this.no_tip;
					var msgtop = uni.getStorageSync('msgnotip') ? uni.getStorageSync('msgnotip') : [];
					if (this.no_tip) {
						msgtop.push('U' + this.userid)
					} else {
						for (var i = 0; i < msgtop.length; i++) {
							if (msgtop[i] == 'U' + this.userid) {
								msgtop.splice(i, 1)
							}
						}
					}
					uni.setStorageSync('msgnotip', msgtop);
					var data = {
						cache_key: 'U' + this.userid,
						userid: this.myid,
						notip: this.no_tip
					};

					http.setWait(false).get('group.php?act=set_msgnotip', data).then(res => {

					})

				}


				if (type == 'backlist') {
					this.backlist = !this.backlist;
					if (this.backlist == true) {
						uni.showModal({
							title: '提示',
							content: '加入黑名单后将无法收到对方的消息\n确认要把' + this.userinfo.nickname + '加入名单？',
							showCancel: true,
							cancelText: '取消',
							confirmText: '确认',
							success: res => {
								if (res.confirm) {
									this.addbacklist();
								} else {
									this.backlist = false;
								}
							}
						});
					} else {
						this.addbacklist();
					}

				}

			},
			addbacklist() {
				http.setWait(false).get('user.php?act=backlist', {
					id: this.userid,
					userid: this.myid
				}).then(res => {
					this.getmyinfo();
					if (this.backlist == true) this.delete_msg('U' + this.userid);
				})
			},
			sub_rename() {
				var data = {
					friend_uid: this.userid,
					userid: this.myid,
					mark: this.markname
				};
				http.setWait(false).get('user.php?act=setmark', data).then(res => {
					uni.showToast({
						title: "备注成功",
						icon: 'none'
					})
					this.getuserinfo(this.userid);
				})

			},
			deleteFriend() {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确定要删除好友吗？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '删除',
					success: res => {
						if (res.confirm) {
							api.deleteFriend({
								friendid: that.userid,
								userid: that.myid
							}).then(res => {
								this.$toast('删除成功').then(() => {

									this.delete_msg('U' + that.userid);
									uni.reLaunch({
										url: '../index/index'
									});
								});
							});
						}
					}
				});
			},
			getmyinfo() {
				var postdata = {
					id: uni.getStorageSync('access_token')
				};
				http.setWait(false).post('user.php?act=userinfo', postdata).then(res => {
					this.myuser = res.data;
					uni.setStorageSync('userInfo', this.myuser)
					var backlist = this.myuser.backlist.toString();
					if (backlist.indexOf(this.userid) > -1) this.backlist = true;
					else this.backlist = false;
				})

			},
			getuserinfo(userid) {

				if (uni.getStorageSync('members_' + userid)) this.userinfo = uni.getStorageSync('members_' + userid);

				http.setWait(false).get('user.php?act=userdetail', {
					id: userid,
					group_id: 0,
					userid: this.myid
				}).then(res => {
					if (res.code == 200) {
						this.isloading = false;

						this.userinfo = res.data;
						this.markname = this.userinfo.nickname;
						uni.setStorageSync('members_' + userid, res.data)
					} else {
						uni.showToast({
							title: '网络错误，请稍后再试',
							icon: 'none'
						})
					}


				})

			},
			openinfo() {
				uni.redirectTo({
					url: 'detail?id=' + this.userid + '&from=chat'
				})
			},
			addgroup() {
				uni.navigateTo({
					url: 'userlist?disabled_id=' + this.userid + '&next=group/create&subtitle=立即创建'
				})
			},
			init() {
				var msgtop = uni.getStorageSync('msgtop');
				var istop = 0;
				for (var i = 0; i < msgtop.length; i++) {
					if (msgtop[i] == 'U' + this.userid) {
						this.istop = true;
						break;
					}
				}

				var msgnotip = uni.getStorageSync('msgnotip');
				for (var i = 0; i < msgnotip.length; i++) {
					if (msgnotip[i] == 'U' + this.userid) {
						this.no_tip = true;
						break;
					}
				}

				var backlist = this.myuser.backlist;
				if (backlist.indexOf(this.userid) > -1) this.backlist = true;
				else this.backlist = false;


			},

		},

		onShow() {

		},
		onLoad(opts) {
			this.userid = opts.id;
			if (opts.from) this.from = opts.from;
			if (uni.getStorageSync('members_' + this.userid)) {
				this.userinfo = uni.getStorageSync('members_' + this.userid);
				this.markname = this.userinfo.nickname;
			}
			this.getuserinfo(this.userid);
			this.init();
		}
	}
</script>
<style lang="scss" scoped>
	@import "@/static/css/user.css";
	page {
		background-color: #e5e5e5;
	}

</style>